#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time    : 2021/4/3 18:30
# @Author  : lxy
import json
import logging
import os
import platform
import time

from SemiCNN_SL_aug import SemiCNN_SL_aug_train
from SemiCNN_SSL_aug import SemiCNN_SSL_aug_train
from TextCNN_SL_aug import TextCNN_SL_aug_train
from Utils import load_passages
from SemiCNN_SSL import SemiCNN_SSL_train
from TextCNN_SSL import TextCNN_SSL_train
from split import generator
from SemiCNN_SL import SemiCNN_SL_train
from TextCNN_SL import TextCNN_SL_train
from word2vec import getAll, train
from vec2tensor import vec2tensor_3D

json_file = "config"

def load_json():
    with open(json_file) as f:
        config = json.load(f)
    list = []
    for cfg_name in config:
        l = []
        l.append(config[cfg_name]["DataName"])
        l.append(config[cfg_name]["categorys"])
        l.append(config[cfg_name]["single"])
        l.append(config[cfg_name]["samples"])
        l.append(config[cfg_name]["T_0"])
        l.append(config[cfg_name]["word_embedding"])
        l.append(config[cfg_name]["epoch"])
        list.append(l)
    return list



""" param : config_list 
    # config_list[0-6]  "THUCNews"  "sougou"  "amazon"
    # config_list[][0-5] examples:
    #{
    #     "DataName": "amazon",
    #     "categorys": 4,
    #     "single": 500,
    #     "samples": [0,200,400],
    #     "T_0": 50
    #}
    """
def Make_Worddict(current,project_path):
    """
    To complect word2vector and constructe word dictionary
    :param current: DataSet Name
    :return: None
    """
    if (platform.system() == 'Windows'):
        bin_path = "D:\\embedding/" + current_name + ".bin"
        csv_path = "D:\\word_dict/" + current_name + ".csv"
    else:
        bin_path = current_path + "/embedding/" + current_name + ".bin"
        csv_path = current_path + "/word_dict/" + current_name + ".csv"
    print(bin_path,csv_path)
    if os.path.exists(bin_path):
        print(bin_path + " is existed")
    else:
        getAll(current, project_path)
        train(current, project_path)
    if os.path.exists(csv_path):
        print(csv_path + " is existed")
    else:
        print("construte word dictionary")
        load_passages(current, project_path)
    return None

def Vector2Tensor(current,project_path):
    """
    To complect vectors2tensors and word2num2tensors
    :param current: DataSet Name
    :return:None
    """
    print(project_path)
    vec2tensor_3D(current,project_path)
    return None


if __name__ == "__main__":
    config_list = load_json()
    step = 3
    while step < 4 :
        config_current = config_list[step]
        current_name = config_current[0]
        current_category = config_current[1]
        current_single = config_current[2]
        current_samples = config_current[3]
        current_T_0 = config_current[4]
        current_embedding = config_current[5]
        current_epoch = config_current[6]
        current_path = os.path.abspath(__file__)
        current_path = os.path.abspath(os.path.dirname(current_path))

        logger = logging.getLogger("train_log")
        logger.setLevel(logging.DEBUG)
        formater = logging.Formatter('%(message)s')
        #
        # print("-"*50+"starting embedding words"+'-'*50)
        # Make_Worddict(current=current_name,project_path=current_path)
        # print("-"*50+"word embedding completed"+'-'*50)
        #
        # Vector2Tensor(current=current_name,project_path=current_path)
        # print("-" * 50 + "vector 2 tensors completed" + '-' * 50)
        # # #
        # generator(category=current_category,num=current_single,idx=current_samples,project_path=current_path )
        # print("-" * 50 + "tensor split completed" + '-' * 50)
        # for i in range(10):
        #     result_time = time.strftime("%Y-%m-%d_%H_%M_%S", time.localtime())
        #     SemiCNN_SL_train(epoch_=current_epoch, dataset=current_name, category=current_category,word_num=current_embedding, T_0=current_T_0, project_path=current_path,single=current_single, logger_=logger, k=5,result_time = result_time)
        #     print("-" * 50 + "SemiCNN SL train completed" + '-' * 50)
        #
        #
        #     SemiCNN_SL_aug_train(epoch_ = current_epoch,dataset=current_name,category=current_category,word_num=current_embedding,T_0=current_T_0,project_path=current_path,single=current_single,logger_ = logger,k=5,result_time = result_time)
        #     print("-" * 50 + "SemiCNN SL_aug train completed" + '-' * 50)
        #
        #     SemiCNN_SSL_train(epoch_ = current_epoch,dataset=current_name,category=current_category,word_num=current_embedding,T_0=current_T_0,project_path=current_path,single=current_single,logger_ = logger,k=5,result_time = result_time)
        #     print("-" * 50 + "SemiCNN SSL train completed" + '-' * 50)
        #
        #     SemiCNN_SSL_aug_train(epoch_=current_epoch, dataset=current_name, category=current_category,word_num=current_embedding, T_0=current_T_0, project_path=current_path, single=current_single,logger_=logger, k=5,result_time = result_time)
        #     print("-" * 50 + "SemiCNN SSL_aug train completed" + '-' * 50)
        for i in range(10):
            result_time = time.strftime("%Y-%m-%d_%H_%M_%S", time.localtime())
            TextCNN_SL_train(epoch_ = current_epoch,dataset=current_name,category=current_category,word_num=current_embedding,T_0=current_T_0,project_path=current_path,single=current_single,logger_ = logger,k=5,result_time = result_time)
            print("-" * 50 + "TextCNN SL train completed" + '-' * 50)

            TextCNN_SL_aug_train(epoch_=current_epoch, dataset=current_name, category=current_category,
                             word_num=current_embedding, T_0=current_T_0, project_path=current_path, single=current_single,
                             logger_=logger, k=5, result_time=result_time)
            print("-" * 50 + "TextCNN SL aug train completed" + '-' * 50)

            TextCNN_SSL_train(epoch_=current_epoch, dataset=current_name, category=current_category,
                              word_num=current_embedding, T_0=current_T_0, project_path=current_path, single=current_single,
                              logger_=logger, k=5, result_time=result_time)
            print("-" * 50 + "TextCNN SSL train completed" + '-' * 50)

            TextCNN_SSL_train(epoch_ = current_epoch,dataset=current_name,category=current_category,word_num=current_embedding,T_0=current_T_0,project_path=current_path,single=current_single,logger_ = logger,k=5,result_time = result_time)
            print("-" * 50 + "TextCNN SSL aug train completed" + '-' * 50)
        step += 1
